<?php
/**
 * Smart Library - Smart Framework
 * http://smart-framework.highet.eu
 *
 * @category   SmartL
 * @package    SmartL_Zend
 * @subpackage Controller
 * @copyright  Copyright (c) 2005-2008 Smart Framework team - Stepan Sindelar, Vojtech Vit
 * @license    http://www.opensource.org/licenses/bsd-license.php	New BSD License
 */

/**
 * @see Zend_Controller_Router_Rewrite
 */
require_once 'Zend/Controller/Router/Rewrite.php';
/**
 * @see SmartL_Application
 */
require_once 'SmartL/Application.php';
/**
 * @see SmartL_Application_Config_LanguageInfo
 */
require_once 'SmartL/Application/Config/LanguageInfo.php';

/**
 * @package SmartL_Zend
 * @subpackage Controller
 */
class SmartL_Zend_Controller_Router_Rewrite extends Zend_Controller_Router_Rewrite {
	const LANGUAGE_ID = 'languageId';
	
	/**
	 * Find a matching route to the current PATH_INFO and inject
	 * returning values to the Request object.
	 *
	 * @param Zend_Controller_Request_Abstract $request
	 * @param SmartL_Application Application singleton, only for testing purposes.
	 * @return Zend_Controller_Request_Abstract Request object
	 */
	public function route(Zend_Controller_Request_Abstract $request, SmartL_Application $application = null) {
		if ($application === null) {
			$application = SmartL_Application::getInstance ();
		}		
		
		if ($request->getPathInfo () != strtolower ( $request->getPathInfo () ) && 
			$application->getAlwaysLowerUrlFlag() ) {
			require_once 'SmartL/Zend/Controller/Router/Rewrite/Exception.php';
			throw new SmartL_Zend_Controller_Router_Rewrite_Exception ( "Url must contain only lower case characters" );
		}
		
		$request = parent::route ( $request );

		if (($langId = $request->getParam ( self::LANGUAGE_ID )) !== null) {
			// Decode language id
			$langId = $this->_decodeLanguageId ( $request->getParam ( self::LANGUAGE_ID ) );
			$request->setParam ( self::LANGUAGE_ID, $langId );
			
			$language = new SmartL_Application_Config_LanguageInfo ( $langId );
			
			$locales = Zend_Locale::getLocaleList ();
			if (isset ( $locales [$language->getZendId ()] )) {
				$application->setUserLanguageWithDefaultCulture ( $language );
			} else {
				require_once 'SmartL/Zend/Controller/Router/Rewrite/Exception.php';
				throw new SmartL_Zend_Controller_Router_Rewrite_Exception ( "Language given in url is not valid." );
			}
		}
		
		return $request;
	}
	/**
	 * Renames language id with specified, lowercase culture (like cs-cz) to
	 * correct ISO format with uppercase culture (like cs-CZ).
	 *
	 * @param string $langId Language id with lowercase culture
	 * @return string Language id with uppercase culture
	 */
	private function _decodeLanguageId($langId) {
		if (strpos ( $langId, '-' ) !== false) {
			$explode = explode ( '-', $langId );
			if (count ( $explode ) == 2) {
				list ( $langId, $cultureId ) = $explode;
				return $langId . '-' . strtoupper ( $cultureId );
			}
		}
		require_once 'SmartL/Zend/Controller/Router/Rewrite/Exception.php';
		throw new SmartL_Zend_Controller_Router_Rewrite_Exception ( "Language given in url is not in valid format." );
	}
}
